/*
 * *****************************************************************************
 *  Copyright ( c ) 2019 huibur Inc. All Rights Reserved.
 *
 *  This software is the confidential and proprietary information of Huibur Inc
 *  (*Confidential Information*). You shall not disclose such Confidential Information
 *  and shall use it only in accordance with the terms of the license agreement
 *  you entered into with Huibur or a Huibur authorized
 *  reseller (the "License Agreement*).
 *  ******************************************************************************
 */

package com.huibur.devicecheck.controller;

import com.huibur.devicecheck.enums.ResultCodeEnum;
import com.huibur.devicecheck.exception.UserDisableException;
import com.huibur.devicecheck.exception.UserNotFoundException;
import com.huibur.devicecheck.exception.UserNotPasswordException;
import com.huibur.devicecheck.model.param.DeviceParam;
import com.huibur.devicecheck.model.param.JudgePowerParam;
import com.huibur.devicecheck.model.param.LoginParam;
import com.huibur.devicecheck.model.po.CheckItemPO;
import com.huibur.devicecheck.model.po.TaskPO;
import com.huibur.devicecheck.model.query.PwdQuery;
import com.huibur.devicecheck.model.query.TaskRecordQuery;
import com.huibur.devicecheck.model.vo.*;
import com.huibur.devicecheck.request.ListRes;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.PageRes;
import com.huibur.devicecheck.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/*******************************************************
* Title: ForAppController.java
* Description:
* @author baiqingshuo
* @date 2021/9/23
* @version 1.0
*******************************************************/
@RestController
@RequestMapping("/apps")
@Api(value = "移动端",tags = {"移动端"})
@Slf4j
public class ForAppController {

    @Autowired
    IForAppService iForAppService;
    @Autowired
    ITaskService iTaskService;
    @Autowired
    ITaskRecordService iTaskRecordService;
    @Autowired
    IDeviceService iDeviceService;
    @Autowired
    IAssignTaskService iAssignTaskService;

    /**
     * 用户根据label获取设备详情
     * @param label
     * @return
     */
    @GetMapping("/selectDeviceByLabel")
    @ApiOperation(value = "用户根据label获取设备详情",response = ObjRes.class)
    public ObjRes<AppVO> selectDeviceByLabel(String label){
        return new ObjRes<AppVO>().data(iForAppService.selectDeviceByLabel(label));
    }

    /**
     * 检修员登录
     * @param loginParam
     * @return
     */
    @PostMapping("/checkLogin")
    @ApiOperation(value = "检修员登录", response = ObjRes.class)
    public ObjRes<UserVO> login(@RequestBody @Valid LoginParam loginParam) {
        try {
            return new ObjRes<UserVO>().data(iForAppService.login(loginParam));
        } catch (UserNotFoundException e) { //用户不存在
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.USER_NOT_FOUND.getCode(), ResultCodeEnum.USER_NOT_FOUND.getLabel());
        } catch (UserNotPasswordException e) { //用户密码错误
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.USER_NOT_PASSWORD.getCode(), ResultCodeEnum.USER_NOT_PASSWORD.getLabel());
        } catch (UserDisableException e) { //用户被禁用
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.USER_DISABLE.getCode(), ResultCodeEnum.USER_DISABLE.getLabel());
        }
    }

    /**
     * 新增巡检记录
     * @param recordVO
     * @return
     */
    @PostMapping("/insertRecord")
    @ApiOperation(value = "新增巡检记录",response = ObjRes.class)
    public ObjRes<Void> insertRecord(@RequestBody RecordVO recordVO) {
        return iForAppService.insertRecord(recordVO);
    }

    /**
     * 修改密码
     * @param pwdQuery
     * @return
     */
    @PutMapping("/updatePwd")
    @ApiOperation(value = "修改密码", response = ObjRes.class)
    public ObjRes<Void> updatePwd(@RequestBody PwdQuery pwdQuery) {
        return iForAppService.updatePwd(pwdQuery);
    }

    /**
     * 任务列表
     * @return
     */
    @GetMapping("/selectTaskList")
    @ApiOperation(value = "任务列表", response = ListRes.class)
    public ListRes<TaskPO> taskList() {
        return new ListRes<TaskPO>().data(iForAppService.taskList());
    }

    /**
     * 检查项列表
     * @return
     */
    @GetMapping("/selectCheckItem")
    @ApiOperation(value = "检查项列表",response = ListRes.class)
    public ListRes<CheckItemPO> selectCheckItem(@RequestParam String label) {
        return new ListRes<CheckItemPO>().data(iForAppService.selectCheckItem(label));
    }

    /**
     * 根据label修改设备信息(设备照片)
     * @param deviceParam
     * @return
     */
    @PutMapping("/updateByLabel")
    @ApiOperation(value = "根据label修改设备信息(设备照片)")
    public ObjRes<Void> updateByLabel(@RequestBody DeviceParam deviceParam) {
        return iForAppService.updateByLabel(deviceParam);
    }

    /**
     * 移动端检修记录列表(查询并分页)
     * @param taskRecordQuery
     * @return
     */
    @GetMapping("/selectTaskRecordList")
    @ApiOperation(value = "移动端检修记录列表(查询并分页)", response = PageRes.class)
    public PageRes<TaskRecordVO> selectTaskRecordList(TaskRecordQuery taskRecordQuery) {
        return iTaskRecordService.selectTaskRecordList(taskRecordQuery);
    }

    /**
     * 获取设备基本信息
     * @param label
     * @return
     */
    @GetMapping("/findDeviceByLabel")
    @ApiOperation(value = "获取设备基本信息", response = ObjRes.class)
    public ObjRes<DeviceVO> findDeviceByLabel(String label) {
        return new ObjRes<DeviceVO>().data(iDeviceService.findDeviceByLabel(label));
    }

    /**
     * 判断是否有提交权限
     * @param judgePowerParam
     * @return
     */
    @GetMapping("/judgePower")
    @ApiOperation(value = "判断是否有提交权限",response = ObjRes.class)
    public ObjRes<Boolean> judgePower(JudgePowerParam judgePowerParam) {
        return new ObjRes<Boolean>().data(iAssignTaskService.judgePower(judgePowerParam));
    }
}
